package cn.stylefeng.guns.modular.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.biz.entity.PrWeightSumOrder;
import cn.stylefeng.guns.modular.biz.enums.PrWeightSumOrderExceptionEnum;
import cn.stylefeng.guns.modular.biz.mapper.PrWeightSumOrderMapper;
import cn.stylefeng.guns.modular.biz.pojo.request.PrWeightSumOrderRequest;
import cn.stylefeng.guns.modular.biz.service.PrWeightSumOrderService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Date;
import java.math.BigDecimal;

/**
 * MES计量汇总单业务实现层
 *
 * @author 张朋飞
 * @since 2024/09/29 21:24
 */
@Service
public class PrWeightSumOrderServiceImpl extends ServiceImpl<PrWeightSumOrderMapper, PrWeightSumOrder> implements PrWeightSumOrderService {

	@Override
    public void add(PrWeightSumOrderRequest prWeightSumOrderRequest) {
        PrWeightSumOrder prWeightSumOrder = new PrWeightSumOrder();
        BeanUtil.copyProperties(prWeightSumOrderRequest, prWeightSumOrder);
        this.save(prWeightSumOrder);
    }

    @Override
    public void del(PrWeightSumOrderRequest prWeightSumOrderRequest) {
        PrWeightSumOrder prWeightSumOrder = this.queryPrWeightSumOrder(prWeightSumOrderRequest);
        this.removeById(prWeightSumOrder.getWsoId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(PrWeightSumOrderRequest prWeightSumOrderRequest) {
        this.removeByIds(prWeightSumOrderRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(PrWeightSumOrderRequest prWeightSumOrderRequest) {
        PrWeightSumOrder prWeightSumOrder = this.queryPrWeightSumOrder(prWeightSumOrderRequest);
        BeanUtil.copyProperties(prWeightSumOrderRequest, prWeightSumOrder);
        this.updateById(prWeightSumOrder);
    }

    @Override
    public PrWeightSumOrder detail(PrWeightSumOrderRequest prWeightSumOrderRequest) {
        return this.queryPrWeightSumOrder(prWeightSumOrderRequest);
    }

    @Override
    public PageResult<PrWeightSumOrder> findPage(PrWeightSumOrderRequest prWeightSumOrderRequest) {
        LambdaQueryWrapper<PrWeightSumOrder> wrapper = createWrapper(prWeightSumOrderRequest);
        Page<PrWeightSumOrder> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public List<PrWeightSumOrder> findList(PrWeightSumOrderRequest prWeightSumOrderRequest) {
        LambdaQueryWrapper<PrWeightSumOrder> wrapper = this.createWrapper(prWeightSumOrderRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 张朋飞
     * @since 2024/09/29 21:24
     */
    private PrWeightSumOrder queryPrWeightSumOrder(PrWeightSumOrderRequest prWeightSumOrderRequest) {
        PrWeightSumOrder prWeightSumOrder = this.getById(prWeightSumOrderRequest.getWsoId());
        if (ObjectUtil.isEmpty(prWeightSumOrder)) {
            throw new ServiceException(PrWeightSumOrderExceptionEnum.PR_WEIGHT_SUM_ORDER_NOT_EXISTED);
        }
        return prWeightSumOrder;
    }

    /**
     * 创建查询wrapper
     *
     * @author 张朋飞
     * @since 2024/09/29 21:24
     */
    private LambdaQueryWrapper<PrWeightSumOrder> createWrapper(PrWeightSumOrderRequest prWeightSumOrderRequest) {
        LambdaQueryWrapper<PrWeightSumOrder> queryWrapper = new LambdaQueryWrapper<>();

        Long wsoId = prWeightSumOrderRequest.getWsoId();
        queryWrapper.eq(ObjectUtil.isNotNull(wsoId), PrWeightSumOrder::getWsoId, wsoId);

        String wtSumId = prWeightSumOrderRequest.getWtSumId();
        queryWrapper.like(ObjectUtil.isNotEmpty(wtSumId), PrWeightSumOrder::getWtSumId, wtSumId);

        String sumDate = prWeightSumOrderRequest.getSumDate();
        queryWrapper.eq(ObjectUtil.isNotNull(sumDate), PrWeightSumOrder::getSumDate, sumDate);

        String purOrderId = prWeightSumOrderRequest.getPurOrderId();
        queryWrapper.like(ObjectUtil.isNotEmpty(purOrderId), PrWeightSumOrder::getPurOrderId, purOrderId);

        String purOrderItemId = prWeightSumOrderRequest.getPurOrderItemId();
        queryWrapper.like(ObjectUtil.isNotEmpty(purOrderItemId), PrWeightSumOrder::getPurOrderItemId, purOrderItemId);

        String l4Matnr = prWeightSumOrderRequest.getL4Matnr();
        queryWrapper.like(ObjectUtil.isNotEmpty(l4Matnr), PrWeightSumOrder::getL4Matnr, l4Matnr);

        String supplyId = prWeightSumOrderRequest.getSupplyId();
        queryWrapper.like(ObjectUtil.isNotEmpty(supplyId), PrWeightSumOrder::getSupplyId, supplyId);

        String transportMode = prWeightSumOrderRequest.getTransportMode();
        queryWrapper.like(ObjectUtil.isNotEmpty(transportMode), PrWeightSumOrder::getTransportMode, transportMode);

        BigDecimal vehNum = prWeightSumOrderRequest.getVehNum();
        queryWrapper.eq(ObjectUtil.isNotNull(vehNum), PrWeightSumOrder::getVehNum, vehNum);

        BigDecimal weight = prWeightSumOrderRequest.getWeight();
        queryWrapper.eq(ObjectUtil.isNotNull(weight), PrWeightSumOrder::getWeight, weight);

        String wtUnit = prWeightSumOrderRequest.getWtUnit();
        queryWrapper.like(ObjectUtil.isNotEmpty(wtUnit), PrWeightSumOrder::getWtUnit, wtUnit);

        String sampleId = prWeightSumOrderRequest.getSampleId();
        queryWrapper.like(ObjectUtil.isNotEmpty(sampleId), PrWeightSumOrder::getSampleId, sampleId);

        String deliverPlace = prWeightSumOrderRequest.getDeliverPlace();
        queryWrapper.like(ObjectUtil.isNotEmpty(deliverPlace), PrWeightSumOrder::getDeliverPlace, deliverPlace);

        String arrId = prWeightSumOrderRequest.getArrId();
        queryWrapper.like(ObjectUtil.isNotEmpty(arrId), PrWeightSumOrder::getArrId, arrId);

        String prueflos = prWeightSumOrderRequest.getPrueflos();
        queryWrapper.like(ObjectUtil.isNotEmpty(prueflos), PrWeightSumOrder::getPrueflos, prueflos);


        return queryWrapper;
    }

}
